package pets

import (
	"github.com/gin-gonic/gin"
	"github.com/xinglou123/pkg/os/conv"
	"github.com/xinglou123/pkg/utils"
	"net/http"
)

type ApiPhrases struct {
}

func (au ApiPhrases) Router(router *gin.Engine) {
	r := router.Group("pets")
	r.GET("phrases/list", au.phrasesList)
	r.GET("phrases/info/:id", au.phrasesInfoWithId)
	r.POST("phrases/edit", au.phrasesEdit)
	r.POST("phrases/delete/:id", au.phrasesDelete)
	r.POST("phrases/create", au.phrasesAdd)

}

// @Title /phrases/list
// @API_GROUP phrases
// @Description 查询列表接口
// @Param   title     query   int true       "title"
// @Param   page     query   int true       "page"
// @Param   pageSize     query   int true       "pageSize"
// @Success 200 {object}
// @router /phrases/list [get]
func (au ApiPhrases) phrasesList(ctx *gin.Context) {
	result := utils.NewResponse()
	defer ctx.JSON(http.StatusOK, result)

	params := map[string]interface{}{}
	params["path"] = ctx.DefaultQuery("path", "")
	params["user_id"] = ctx.DefaultQuery("user_id", "")
	params["limit"] = ctx.DefaultQuery("limit", "10")
	params["page"] = ctx.DefaultQuery("page", "1")

	list, page, err := PhraseService.Query(params)
	if err != nil {
		result.Code = 100
		result.Msg = err.Error()
		return
	}
	data := map[string]interface{}{}
	data["lists"] = list
	data["page"] = page

	result.Data = data
	result.Code = 200
	result.Msg = "ok"

	return

}

// @Title /phrases/info/:id
// @API_GROUP user
// @Description 查询列表接口
// @Param   uid     query   string false       "uid"
// @Success 200 {object}
// @router /phrases/info/:id [get]
func (au ApiPhrases) phrasesInfoWithId(ctx *gin.Context) {
	result := utils.NewResponse()
	defer ctx.JSON(http.StatusOK, result)

	uid := ctx.Param("id")
	if uid == "" {
		result.Code = 100
		result.Msg = "参数异常"
		return
	}
	word, err := PhraseService.One(conv.Int64(uid))
	if err != nil {
		result.Code = 100
		result.Msg = err.Error()
		return
	}
	result.Data = word
	return
}

// @Title phrases add
// @API_GROUP user
// @Description 添加接口
// @Param   SignupdateForm
// @Success 200 {object}
// @router /phrases/add [post]
func (au ApiPhrases) phrasesAdd(ctx *gin.Context) {
	result := utils.NewResponse()
	defer ctx.JSON(http.StatusOK, result)

	var word Phrase
	if error := ctx.Bind(&word); error != nil {
		result.Code = 100
		result.Msg = "参数异常"
		return
	}
	uuser, err := PhraseService.Add(word)
	if err != nil {
		result.Code = 100
		result.Msg = err.Error()
		return
	}
	result.Msg = "添加用户成功"
	result.Data = uuser
	return

}

// @Title phrases Delete
// @API_GROUP user
// @Description 删除接口
// @Param   id     query   int true       "id"
// @Success 200 {object}
// @router /phrases/delete [post]
func (au ApiPhrases) phrasesDelete(ctx *gin.Context) {
	result := utils.NewResponse()
	defer ctx.JSON(http.StatusOK, result)

	uid := ctx.Param("id")
	if uid == "" {
		result.Code = 100
		result.Msg = "参数异常"
		return
	}
	_, err := PhraseService.Delete(conv.Int64(uid))
	if err != nil {
		result.Code = 100
		result.Msg = "删除用户失败"
		return
	}
	result.Msg = "删除用户成功"
	return
}

// @Title phrases edit
// @API_GROUP words
// @Description 编辑接口
// @Param   SignupdateForm
// @Success 200 {object}
// @router /phrases/edit [post]
func (au ApiPhrases) phrasesEdit(ctx *gin.Context) {
	result := utils.NewResponse()
	defer ctx.JSON(http.StatusOK, result)

	var word Phrase
	if err := ctx.Bind(&word); err != nil {
		result.Code = 100
		result.Msg = err.Error()
		return
	}
	if word.Id == 0 {
		result.Code = 100
		result.Msg = "请选择要修改的用户"
		return
	}
	_, err := PhraseService.Update(word)
	if err != nil {
		result.Code = 100
		result.Msg = "修改用户失败"
		return
	}
	result.Msg = "修改用户成功"
	return
}
